let timer
let duration

self.onmessage = function (e) {
  if (e.data.command === 'start') {
    duration = e.data.duration
    startTimer()
  }
}

function startTimer() {
  timer = setInterval(function () {
    duration--

    // 向主线程发送剩余时间
    self.postMessage({ time: duration })

    if (duration <= 0) {
      clearInterval(timer)
      // 向主线程发送时间到的消息
      self.postMessage({ time: 0 })
    }
  }, 1000)
}
